using System.Collections.Specialized;
using FlickrNet;
using System.Linq;
using WroxGallery.Models.Interfaces;

namespace WroxGallery.Models.Implementations
{
    public class FlickrGalleryRepository: IGalleryRepository 
    {
        private IFlickrAPI _flickr;
        private IConfigurationRepository _config;


        public FlickrGalleryRepository(IConfigurationRepository config, IFlickrAPI flickr)
        {
            string apiKey = "2667418393c079aa7ba575d19a4c09d6";
            _config = config;
            _flickr = flickr;
            _flickr.APIKey = apiKey;
        }

        public PhotoCollection GetLatestPhoto(int num)
        {
            Photos userPhotos = _flickr.PhotosSearch(_config.FickerUserName, num);
            return GetPhotoCollection(userPhotos.PhotoCollection);
        }

        public PhotoCollection GetPhotosInAlbum(string albumId, int size)
        {
            Photoset photoset = _flickr.PhotosetsGetPhotos(albumId, size);
            return GetPhotoCollection(photoset.PhotoCollection);
        }

        public PhotoCollection SearchPhotosByTag(string tag, int size)
        {
            Photos userPhotos = _flickr.PhotosSearch(_config.FickerUserName, tag, size);
            return GetPhotoCollection(userPhotos.PhotoCollection);
        }

        public AlbumCollection GetAlbums(int num)
        {
            var photoSets = _flickr.PhotosetsGetList(_config.FickerUserName);
            var album = from ps in photoSets.PhotosetCollection
                        orderby ps.PhotosetId descending
                        select new Album
                                   {
                                       Title = ps.Title,
                                       Id = ps.PhotosetId,
                                       Url = ps.PhotosetSquareThumbnailUrl
                                   };

            return new AlbumCollection(album.Take(num));
        }

        private PhotoCollection GetPhotoCollection(FlickrNet.Photo[] photos)
        {
            PhotoCollection collection = new PhotoCollection();
            foreach (FlickrNet.Photo photo in photos)
            {
                Photo mvcPhoto = new Photo()
                             {
                                 Title = photo.Title,
                                 Description = 
                                    _flickr.PhotosGetInfo(photo.PhotoId).Description,
                                 ThumbUrl = photo.SmallUrl,
                                 Url = photo.MediumUrl
                             };
                collection.Add(mvcPhoto);
            }
            return collection;
        }

        public StringCollection TagList(string search)
        {
            var tags = _flickr.GetTags(_config.FickerUserName);
            var searched = from t in tags
                           orderby t
                           where t.StartsWith(search)
                           select t;
            var retVal = new StringCollection();
            retVal.AddRange(searched.ToArray());
            return retVal;
        }
    }
}